//! 配置相关定义

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Duration;
use crate::NacosError;

/// 顶层应用配置
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AppConfig {
    pub server: ServerConfig,
    pub cluster: ClusterConfig,
    pub storage: StorageConfig,
    pub network: NetworkConfig,
}

impl AppConfig {
    /// 从文件加载配置 (占位符)
    pub fn load_from_file(_path: &str) -> Result<Self, NacosError> {
        // 在未来，这里将使用 config-rs 或 figment 等库来实现
        // 目前，我们只返回默认配置
        Ok(Self::default())
    }
}

/// Nacos服务器配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
    pub address: String,
    pub http_port: u16,
    pub grpc_port: u16,
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            address: "0.0.0.0".to_string(),
            http_port: 8848,
            grpc_port: 9848,
        }
    }
}

/// 集群配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClusterConfig {
    pub cluster_name: String,
    pub nodes: Vec<String>,
    pub heartbeat_interval: Duration,
    pub election_timeout: Duration,
}

impl Default for ClusterConfig {
    fn default() -> Self {
        Self {
            cluster_name: "DEFAULT_CLUSTER".to_string(),
            nodes: vec![],
            heartbeat_interval: Duration::from_secs(5),
            election_timeout: Duration::from_secs(15),
        }
    }
}

/// 存储配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageConfig {
    pub storage_type: StorageType,
    pub connection_string: String,
    pub pool_size: u32,
    pub connection_timeout: Duration,
    pub properties: HashMap<String, String>,
}

impl Default for StorageConfig {
    fn default() -> Self {
        Self {
            storage_type: StorageType::Memory,
            connection_string: String::new(),
            pool_size: 10,
            connection_timeout: Duration::from_secs(30),
            properties: HashMap::new(),
        }
    }
}

/// 存储类型
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum StorageType {
    Memory,
    File,
    Sqlite,
    Mysql,
    PostgreSQL,
}

/// 网络配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkConfig {
    pub connect_timeout: Duration,
    pub read_timeout: Duration,
    pub write_timeout: Duration,
    pub max_connections: u32,
    pub keep_alive: Duration,
}

impl Default for NetworkConfig {
    fn default() -> Self {
        Self {
            connect_timeout: Duration::from_secs(10),
            read_timeout: Duration::from_secs(30),
            write_timeout: Duration::from_secs(30),
            max_connections: 1000,
            keep_alive: Duration::from_secs(60),
        }
    }
}

/// 命名服务配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NamingConfig {
    pub namespace: String,
    pub group: String,
    pub ephemeral_instance_ttl: Duration,
    pub persistent_instance_ttl: Duration,
    pub health_check_interval: Duration,
    pub health_check_timeout: Duration,
}

impl Default for NamingConfig {
    fn default() -> Self {
        Self {
            namespace: "public".to_string(),
            group: "DEFAULT_GROUP".to_string(),
            ephemeral_instance_ttl: Duration::from_secs(30),
            persistent_instance_ttl: Duration::from_secs(300),
            health_check_interval: Duration::from_secs(5),
            health_check_timeout: Duration::from_secs(3),
        }
    }
}

/// 配置服务配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigConfig {
    pub namespace: String,
    pub group: String,
    pub max_config_size: usize,
    pub config_cache_size: usize,
    pub listener_queue_size: usize,
}

impl Default for ConfigConfig {
    fn default() -> Self {
        Self {
            namespace: "public".to_string(),
            group: "DEFAULT_GROUP".to_string(),
            max_config_size: 1024 * 1024, // 1MB
            config_cache_size: 1000,
            listener_queue_size: 10000,
        }
    }
}